$(document).ready(function() {
	toggle()
	//slider
	showActive();
	$('#home-slider').carousel().on('slid.bs.carousel', function () {
		showActive()
	})
	//fin slider
	
	//menu offcanvas
	if (!$('.cbp-spmenu').length) $('#showLeftPush').hide();
	$('#showLeftPush').click(function () {
		$('body').addClass('cbp-spmenu-push-toright');
		$('#menu-offcanvas').addClass('cbp-spmenu-open');
	})
	$('#hideLeftPush').click(function () {
		$('body').removeClass('cbp-spmenu-push-toright');
		$('#menu-offcanvas').removeClass('cbp-spmenu-open');
	})
	//fin menu offcanvas	
	
	//dropdown
	$('.dropdown-link')
		.mouseover(function () {
			if ($(window).width() > 992) {
				var target = $(this).data('target');
				clearTimeout(window['time-'+target]);
				if ($('.dropdown-target.open:not(#'+target+')').length) {
					$('.dropdown-target.open:not(#'+target+')').hide();
					if (!$('#'+target).hasClass('open')) $('#'+target).show();
				} else {
					if (!$('#'+target).hasClass('open'))  {
						window['delay-'+target] = setTimeout(function () {
							$('#'+target).fadeIn(300);
						}, 233)
					}
				}
				$('#'+target).addClass('open');
			}
		})
		.mouseout(function () {
			var target = $(this).data('target');
			clearTimeout(window['delay-'+target]);
			window['time-'+target] = setTimeout(
			function () {
				$('#'+target).fadeOut(150);
				$('#'+target).removeClass('open');
			}, 
			150)
		})
	$('.dropdown-target')
		.mouseenter(function () {
			var target = $(this).attr('id');
			clearTimeout(window['time-'+target]);
		})
		.mouseleave(function () {
			var target = $(this).attr('id');
			clearTimeout(window['delay-'+target]);
			window['time-'+target] = setTimeout(
			function () {
				$('#'+target).fadeOut(150);
				$('#'+target).removeClass('open');
			}, 
			150)
		})
	//fin dropdown
})
//slider
function showActive () {
	var active = $('#home-slider .item.active').data('target');
	$('.home-caption').addClass('hide');
	$(active).removeClass('hide');
}
//fin slider

function toggle() {

	var $_t = this,
		previewParClosedHeight = 25;

	jQuery("div.toggle.active > p").addClass("preview-active");
	jQuery("div.toggle.active > div.toggle-content").slideDown(400);
	jQuery("div.toggle > label").click(function(e) {

		var parentSection 	= jQuery(this).parent(),
			parentWrapper 	= jQuery(this).parents("div.toogle"),
			previewPar 		= false,
			isAccordion 	= parentWrapper.hasClass("toogle-accordion");

		if(isAccordion && typeof(e.originalEvent) != "undefined") {
			parentWrapper.find("div.toggle.active > label").trigger("click");
		}

		parentSection.toggleClass("active");

		if(parentSection.find("> p").get(0)) {

			previewPar 					= parentSection.find("> p");
			var previewParCurrentHeight = previewPar.css("height");
			var previewParAnimateHeight = previewPar.css("height");
			previewPar.css("height", "auto");
			previewPar.css("height", previewParCurrentHeight);

		}

		var toggleContent = parentSection.find("> div.toggle-content");

		if(parentSection.hasClass("active")) {

			jQuery(previewPar).animate({height: previewParAnimateHeight}, 350, function() {jQuery(this).addClass("preview-active");});
			toggleContent.slideDown(350);

		} else {

			jQuery(previewPar).animate({height: previewParClosedHeight}, 350, function() {jQuery(this).removeClass("preview-active");});
			toggleContent.slideUp(350);

		}

	});
}